import Vue from 'vue'
import Vuex from 'vuex'
import {getItem, removeItem, setItem} from "@/utils/auth";
import {loginApi} from "@/api/apiFox";


import {encrypt} from "@/utils/AES";

Vue.use(Vuex)
const userKey = 'user'
const tokenKey = 'admin'

export default new Vuex.Store({
    state: {
        token: getItem(tokenKey),
    },
    getters: {},
    mutations: {
        // 更新token
        saveToken(state, data) {
            sessionStorage.setItem('tips',111)
            state.token = data.token
            setItem(userKey, encrypt(JSON.stringify(state)))
            setItem(tokenKey, data.token)
        },


    },
    actions: {
        loginSetToken({commit}, params) {
            return new Promise((resolve, reject) => {
                loginApi(encrypt(params.username.toString()), encrypt(params.password.toString())).then(res => {
                    commit('saveToken', res)
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },


    },
    modules: {}
})
